Skip to content

Conversation

@adityacosmos24
Copy link
Contributor

ATM CASH DESPENSER- Chain of Responsibility Pattern Implementation (C++)

Overview

This PR introduces a C++ implementation of the Chain of Responsibility design pattern, demonstrating its application in an ATM money dispenser system. The implementation showcases how requests (money withdrawal) are passed through a chain of handlers (different denomination handlers) until the request is fully processed or cannot be fulfilled.

Design Pattern

Chain of Responsibility Pattern - A behavioral design pattern that allows multiple objects to handle a request without coupling the sender to specific receivers. Each handler in the chain decides whether to process the request or pass it to the next handler.

Implementation Details

Components

  1. Abstract Handler (MoneyHandler)

    • Base class defining the interface for all handlers
    • Maintains a reference to the next handler in the chain
    • Declares pure virtual dispense() method
  2. Concrete Handlers

    • ThousandHandler: Handles ₹1000 notes
    • FiveHundredHandler: Handles ₹500 notes
    • TwoHundredHandler: Handles ₹200 notes
    • HundredHandler: Handles ₹100 notes
  3. Handler Chain Setup

    • Handlers are linked in descending order of denomination (₹1000 → ₹500 → ₹200 → ₹100)
    • Each handler processes its portion and forwards the remainder to the next handler

Key Features

  • Dynamic Chain Construction: Handlers can be linked in any order
  • Inventory Management: Each handler tracks available notes and dispenses accordingly
  • Graceful Degradation: If a handler cannot fulfill the full amount, it dispenses what it can and passes the remainder
  • Error Handling: Displays appropriate messages when funds are insufficient

Example Usage

The implementation demonstrates:

  • Initializing handlers with available note counts:
    • ₹1000 notes: 3
    • ₹500 notes: 5
    • ₹200 notes: 10
    • ₹100 notes: 20
  • Processing a withdrawal request of ₹4000
  • Each handler dispenses the maximum possible notes of its denomination
  • Remaining amount is passed to the next handler in the chain

Build Instructions

The project includes a compile.bat script for Windows that:

  • Compiles the C++ code using g++ with C++11 standard
  • Automatically runs the executable upon successful compilation
  • Provides error feedback if compilation fails

Manual Compilation

g++ -std=c++11 -o cor COR.cpp
./cor

Files Included

  • COR.cpp: Main implementation file containing all handler classes and client code
  • compile.bat: Windows batch script for compilation and execution
  • cor.exe: Compiled executable (generated after compilation)

Benefits of This Implementation

  1. Separation of Concerns: Each handler is responsible for a single denomination
  2. Extensibility: Easy to add new denominations by creating new handler classes
  3. Flexibility: Chain order can be modified without changing individual handlers
  4. Maintainability: Clear structure makes the code easy to understand and modify

Learning Outcomes

This implementation demonstrates:

  • How to structure a Chain of Responsibility pattern
  • Virtual function usage for polymorphism
  • Dynamic chain construction and traversal
  • Real-world application of design patterns in financial systems

@prajwal3114 prajwal3114 merged commit 4f4415b into OPCODE-Open-Spring-Fest:main Nov 15, 2025
4 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants